package cn.edu.scau.sec.rw.client;

import cn.edu.scau.sec.rw.controllers.CommonBridge;
import cn.edu.scau.sec.rw.model.disk_file.disk.Disk;
import cn.edu.scau.sec.rw.model.disk_file.file.ExeFile;
import cn.edu.scau.sec.rw.model.disk_file.file.FileCatalog;
import cn.edu.scau.sec.rw.model.disk_file.file.TextFile;
import cn.edu.scau.sec.rw.model.disk_file.utils.File_DiskUtil;
import cn.edu.scau.sec.rw.model.pro_manage.Manager;
import cn.edu.scau.sec.rw.model.pro_manage.threadExe.CPUThread;
import cn.edu.scau.sec.rw.model.pro_manage.threadExe.SysTimeThread;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Paint;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.util.ArrayList;
import java.util.Random;

public class Client extends Application {

    double x = 0;
    double y = 0;

    public static void main(String[] args) {
        Manager manager = new Manager();
        manager.startCPUThread();
        manager.startSystemTimeThread();
        manager.startDeviceThread();
        initData();
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        initStage(primaryStage, "/cn/edu/scau/sec/rw/view/fxml/ClientUI.fxml", "cn/edu/scau/sec/rw/view/css/css.css");
    }

    private void initStage(Stage primaryStage, String fxml, String css){
        Stage stage = primaryStage;
        try {
            Parent root = FXMLLoader.load(getClass().getResource(fxml));
            Scene scene = new Scene(root);
            scene.getStylesheets().add(css);
            scene.setFill(Paint.valueOf("#00000000"));
//            URL url =getClass().getClassLoader().getResource("icon/鼠标.png");
//            String path = url.toExternalForm();
//            scene.setCursor(Cursor.cursor(path));
            stage.setScene(scene);
            stage.setTitle("PhotoManager");
            stage.initStyle(StageStyle.TRANSPARENT);
            CommonBridge.primaryStage = stage;
            stage.show();
            stage.getIcons().add(new Image("/cn/edu/scau/sec/rw/view/img/操作系统.png"));
            scene.setOnMousePressed(new EventHandler<MouseEvent>() {

                @Override
                public void handle(MouseEvent event) {
                    // TODO 自动生成的方法存根
                    if(event.getButton().name().equals(MouseButton.PRIMARY.name())){
                        x = event.getScreenX()-stage.getX();
                        y = event.getScreenY()-stage.getY();
                    }
                }
            });

            scene.setOnMouseDragged(new EventHandler<MouseEvent>() {

                @Override
                public void handle(MouseEvent event) {
                    // TODO 自动生成的方法存根
                    if(event.getButton().name().equals(MouseButton.PRIMARY.name())&&(event.getScreenX()<=
                            Screen.getPrimary().getVisualBounds().getMaxX()||event.getScreenY()<=Screen.getPrimary().getVisualBounds().getMaxY())){
                        if(event.getScreenX()<=Screen.getPrimary().getVisualBounds().getMaxX()) stage.setX(event.getScreenX()-x);
                        if(event.getScreenY()<=Screen.getPrimary().getVisualBounds().getMaxY()) stage.setY(event.getScreenY()-y);
                    }
                }
            });
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    private static void initData(){

        String randomString = null;
        ArrayList randomStatement = null;

//        new CPUThread().start();
//        new SysTimeThread().start();

        FileCatalog fileCatalog1 = new FileCatalog("c","目录项");
        FileCatalog fileCatalog2 = new FileCatalog("d","目录项");
        FileCatalog fileCatalog3 = new FileCatalog("e","目录项");
        FileCatalog fileCatalog4 = new FileCatalog("python","目录项");
        FileCatalog fileCatalog5 = new FileCatalog("java","目录项");
        FileCatalog fileCatalog6 = new FileCatalog("video","目录项");
        FileCatalog fileCatalog7 = new FileCatalog("music","目录项");
        FileCatalog fileCatalog8 = new FileCatalog("myBatis","目录项");
        FileCatalog fileCatalog9 = new FileCatalog("MySQL","目录项");
        FileCatalog fileCatalog10 = new FileCatalog("cxq","目录项");

        randomString = getRandomString();
        TextFile textFile = new TextFile("wo", randomString, (int) randomString.length()/3);
        randomString = getRandomString();
        TextFile textFile2 = new TextFile("ai", randomString, (int) randomString.length()/3);
        randomString = getRandomString();
        TextFile textFile3 = new TextFile("os", randomString, (int) randomString.length()/3);
        randomString = getRandomString();
        TextFile textFile4 = new TextFile("ke", randomString, (int) randomString.length()/3);
        randomString = getRandomString();
        TextFile textFile5 = new TextFile("she", randomString, (int) randomString.length()/3);

        randomStatement = getRandomStatements();
        ExeFile exeFile = new ExeFile("os.e", randomStatement, randomStatement.size());
        randomStatement = getRandomStatements();
        ExeFile exeFile2 = new ExeFile("cxq.e", randomStatement, randomStatement.size());
        randomStatement = getRandomStatements();
        ExeFile exeFile3 = new ExeFile("java.e", randomStatement, randomStatement.size());
        randomStatement = getRandomStatements();
        ExeFile exeFile4 = new ExeFile("c.e", randomStatement, randomStatement.size());
        randomStatement = getRandomStatements();
        ExeFile exeFile5 = new ExeFile("new.e", randomStatement, randomStatement.size());
        randomStatement = getRandomStatements();
        ExeFile exeFile6 = new ExeFile("class.e", randomStatement, randomStatement.size());
        randomStatement = getRandomStatements();
        ExeFile exeFile7 = new ExeFile("player.e", randomStatement, randomStatement.size());
        randomStatement = getRandomStatements();
        ExeFile exeFile8 = new ExeFile("music.e", randomStatement, randomStatement.size());
        randomStatement = getRandomStatements();
        ExeFile exeFile9 = new ExeFile("relax.e", randomStatement, randomStatement.size());
        randomStatement = getRandomStatements();
        ExeFile exeFile10 = new ExeFile("study.e", randomStatement, randomStatement.size());

        FileCatalog root = Disk.getDisk().getRoot();

        root.add(fileCatalog1);
        File_DiskUtil.saveVirtualFile(fileCatalog1);
        root.add(textFile);
        File_DiskUtil.saveVirtualFile(textFile);
        root.add(textFile2);
        File_DiskUtil.saveVirtualFile(textFile2);
        root.add(textFile3);
        File_DiskUtil.saveVirtualFile(textFile3);
        root.add(fileCatalog3);
        File_DiskUtil.saveVirtualFile(fileCatalog3);
        root.add(fileCatalog5);
        File_DiskUtil.saveVirtualFile(fileCatalog5);
        fileCatalog1.add(fileCatalog2);
        File_DiskUtil.saveVirtualFile(fileCatalog2);
        fileCatalog2.add(textFile4);
        File_DiskUtil.saveVirtualFile(textFile4);
        fileCatalog2.add(textFile5);
        File_DiskUtil.saveVirtualFile(textFile5);
        fileCatalog2.add(fileCatalog4);
        File_DiskUtil.saveVirtualFile(fileCatalog4);
        fileCatalog4.add(exeFile);
        File_DiskUtil.saveVirtualFile(exeFile);
        fileCatalog4.add(exeFile2);
        File_DiskUtil.saveVirtualFile(exeFile2);
        fileCatalog4.add(exeFile3);
        File_DiskUtil.saveVirtualFile(exeFile3);
        fileCatalog4.add(exeFile4);
        File_DiskUtil.saveVirtualFile(exeFile4);
        fileCatalog4.add(exeFile5);
        File_DiskUtil.saveVirtualFile(exeFile5);
        fileCatalog3.add(fileCatalog6);
        File_DiskUtil.saveVirtualFile(fileCatalog6);
        fileCatalog1.add(fileCatalog7);
        File_DiskUtil.saveVirtualFile(fileCatalog7);
        fileCatalog4.add(fileCatalog8);
        File_DiskUtil.saveVirtualFile(fileCatalog8);
        fileCatalog8.add(fileCatalog9);
        File_DiskUtil.saveVirtualFile(fileCatalog9);
        fileCatalog5.add(fileCatalog10);
        File_DiskUtil.saveVirtualFile(fileCatalog10);
        fileCatalog4.add(exeFile6);
        File_DiskUtil.saveVirtualFile(exeFile6);
        fileCatalog5.add(exeFile7);
        File_DiskUtil.saveVirtualFile(exeFile7);
        fileCatalog10.add(exeFile8);
        File_DiskUtil.saveVirtualFile(exeFile8);
        fileCatalog9.add(exeFile9);
        File_DiskUtil.saveVirtualFile(exeFile9);
        fileCatalog6.add(exeFile10);
        File_DiskUtil.saveVirtualFile(exeFile10);

//        int[] fat = Disk.getDisk().getFAT();
//        for (int i = 0; i < fat.length; i++) {
//            System.out.println(fat + " ");
//        }
    }

    private static String getRandomString() {

        char[] table = new char[]{'a','b','c','d','e','1','2','3','4','5'};

        int length = (int) Math.random()*1000;
        StringBuffer stringBuffer = new StringBuffer();

        for (int i = 0; i < length; i++) {
            stringBuffer.append(table[(int) Math.random()*table.length]);
        }
        System.out.println(stringBuffer.toString());
        return stringBuffer.toString();
    }

    private static ArrayList<String> getRandomStatements(){

        ArrayList<String> statements =  new ArrayList<>();

        statements.add("x="+ (int) Math.random()*150);

        String[] sts = new String[]{"x++", "x--", "x++", "x--", "x++", "x--", "x++", "x--",
                "x++", "x--", "x++", "x--", "x++", "x--", "x++", "x--", "x++", "x--",
                "x++", "x--", "x++", "x--", "!A4", "!B6", "!C2", "!A2", "!B4", "!C4"};

        int length = new Random().nextInt(100);
        System.out.println(length + " ");

        for (int i = 1; i < length-1; i++) {
            statements.add(sts[(int) new Random().nextInt(sts.length)]);
        }
        statements.add("end");
        for (String statement : statements) {
            System.out.print(statement + " ");
        }
        return statements;
    }
}
